﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ControleBox
{
    public partial class ControleBox : UserControl
    {
        #region Attributs
        private Dictionary<string, Control> _index;
        #endregion

        #region Constructeur
        public ControleBox()
        {
            InitializeComponent();

            _index = new Dictionary<string, Control>();
        }
        #endregion

        #region Proprietes
        [Category("Controles"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Control this[int id]
        {
            get { return this.flowLayoutPanel1.Controls[id]; }
        }
        #endregion

        #region Methodes
        public Controle<U> CreateControl<U>()
            where U : Control, new()
        {
            Controle<U> c = new Controle<U>();
            c.Anchor = (AnchorStyles)(AnchorStyles.Left |
                                    AnchorStyles.Right |
                                    AnchorStyles.Top);

            this.CreateControl<U>(c);

            return c;
        }

        public Controle<U> CreateControl<U>(Controle<U> c)
            where U : Control, new()
        {
            this.flowLayoutPanel1.Controls.Add(c);
            Control ctrl = (Control)c.Control;

            c.Label.Text = IsExist(c.Label.Text);

            _index.Add(c.Label.Text, c.Control);

            return c;
        }

        public bool removeControl<U>(Controle<U> c)
            where U : Control, new()
        {
            if (!flowLayoutPanel1.Controls.Contains(c))
                return false;

            this._index.Remove(c.Label.Text);
            flowLayoutPanel1.Controls.Remove(c);
            c.Dispose();
            return true;

        }

        public bool MoveUp<U>(Controle<U> c)
            where U : Control, new()
        {
            if (!flowLayoutPanel1.Controls.Contains(c))
                return false;

            if (flowLayoutPanel1.Controls.IndexOf(c) == 0)
                return false;

            Control[] co = new Control[flowLayoutPanel1.Controls.Count];
            flowLayoutPanel1.Controls.CopyTo(co, 0);

            flowLayoutPanel1.Controls.Clear();

            Control last = co[0];
            for (int i = 1; i < co.Length; ++i)
            {

            }

            return true;
        }

        private string IsExist(string label)
        {

            if (!_index.ContainsKey(label))
                return label;

            string result = label;
            int i = 1;

            while (_index.ContainsKey(result + i.ToString()))
            {
                label = result;
                ++i;
                label += i.ToString();
            }
            result += i.ToString();
            return result;
        }
        #endregion
    }
}
